package com.zhss.eshop.membership.command;

import com.zhss.eshop.common.constant.CollectionSize;
import com.zhss.eshop.common.util.DateProvider;
import com.zhss.eshop.membership.constant.MemberLevel;
import com.zhss.eshop.membership.constant.UpdateMemberLevelResult;
import com.zhss.eshop.membership.constant.UpdateMemberPointResult;
import com.zhss.eshop.membership.domain.model.MembershipMemberLevel;
import com.zhss.eshop.membership.domain.model.MembershipMemberPoint;
import com.zhss.eshop.membership.mapper.MembershipMemberLevelDetailMapper;
import com.zhss.eshop.membership.mapper.MembershipMemberLevelMapper;
import com.zhss.eshop.membership.mapper.MembershipMemberPointDetailMapper;
import com.zhss.eshop.membership.mapper.MembershipMemberPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 每天第一次登录的会员信息更新组件
 * @author suyuan
 *
 */
@Component
public class FirstLoginMembershipUpdater extends AbstractMembershipUpdater<Object, Boolean> {

	/**
	 * 每天第一次登录对成长值的累加值
	 */
	private static final Long FIRST_LOGIN_GROWTH_VALUE_UPDATE = 5L;
	/**
	 * 每天第一次登录对会员积分的累加值
	 */
	private static final Long FIRST_LOGIN_MEMBER_PONIT_UPDATE = 5L;

	/**
	 * 会员等级管理DAO组件
	 */
	@Autowired
	private MembershipMemberLevelMapper memberLevelDAO;
	/**
	 * 会员积分管理DAO组件
	 */
	@Autowired
	private MembershipMemberPointMapper memberPointDAO;
	/**
	 * 日期辅助组件
	 */
	@Autowired
	private DateProvider dateProvider;

	@Autowired
	public FirstLoginMembershipUpdater(
			MembershipMemberLevelDetailMapper memberLevelDetailDAO,
			MembershipMemberPointDetailMapper memberPointDetailDAO,
			DateProvider dateProvider) {
		super(memberLevelDetailDAO, memberPointDetailDAO,dateProvider);
	}

	/**
	 * 更新会员等级`
	 */
	@Override
	protected Map<String, Object> updateMemberLevel(Long userAccountId,
			Object parameter) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>(CollectionSize.DEFAULT);
		//没有会新增一条记录
		//先查出来，搞两个旧的值存着，做更新操作，在放三个新的存起来
		MembershipMemberLevel memberLevel = memberLevelDAO.getByUserAccountId(userAccountId);
		if(memberLevel == null) {
			memberLevel = new MembershipMemberLevel();
			memberLevel.setUserAccountId(userAccountId);
			memberLevel.setGrowthValue(0L);
			memberLevel.setLevel(MemberLevel.BRONZE);
			memberLevel.setGmtCreate(dateProvider.getCurrentTime());
			memberLevel.setGmtModified(dateProvider.getCurrentTime());
			memberLevelDAO.insertSelective(memberLevel);
		}

		result.put(UpdateMemberLevelResult.OLD_GROWTH_VALUE, memberLevel.getGrowthValue());
		result.put(UpdateMemberLevelResult.OLD_MEMBER_LEVEL, memberLevel.getLevel());

		memberLevel.setGrowthValue(memberLevel.getGrowthValue()
				+ FIRST_LOGIN_GROWTH_VALUE_UPDATE);
		memberLevel.setLevel(MemberLevel.get(memberLevel.getGrowthValue()));
		memberLevel.setGmtModified(dateProvider.getCurrentTime());
		memberLevelDAO.updateByPrimaryKeySelective(memberLevel);

		result.put(UpdateMemberLevelResult.UPDATED_GROWTH_VALUE,
				FIRST_LOGIN_GROWTH_VALUE_UPDATE);
		result.put(UpdateMemberLevelResult.NEW_GROWTH_VALUE, memberLevel.getGrowthValue());
		result.put(UpdateMemberLevelResult.NEW_MEMBER_LEVEL, memberLevel.getLevel());

		return result;
	}

	/**
	 * 更新会员积分
	 */
	@Override
	protected Map<String, Object> updateMemberPoint(Long userAccountId,
			Object parameter) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>(CollectionSize.DEFAULT);

		MembershipMemberPoint memberPoint = memberPointDAO.getByUserAccountId(userAccountId);
		if(memberPoint == null) {
			memberPoint = new MembershipMemberPoint();
			memberPoint.setUserAccountId(userAccountId);
			memberPoint.setPoint(0L);
			memberPoint.setGmtCreate(dateProvider.getCurrentTime());
			memberPoint.setGmtModified(dateProvider.getCurrentTime());
			memberPointDAO.insertSelective(memberPoint);
		}
		result.put(UpdateMemberPointResult.OLD_MEMBER_POINT, memberPoint.getPoint());

		memberPoint.setPoint(memberPoint.getPoint() + FIRST_LOGIN_MEMBER_PONIT_UPDATE);
		memberPoint.setGmtModified(dateProvider.getCurrentTime());
		memberPointDAO.updateByPrimaryKeySelective(memberPoint);

		result.put(UpdateMemberPointResult.UPDATED_MEMBER_POINT,
				FIRST_LOGIN_MEMBER_PONIT_UPDATE);
		result.put(UpdateMemberPointResult.NEW_MEMBER_POINT, memberPoint.getPoint());

		return result;
	}

	/**
	 * 获取会员等级更新原因
	 */
	@Override
	protected String getMemberLevelUpdateReason(Long userAccountId, Object parameter,
			Map<String, Object> updateMemberLevelResult) throws Exception {
		return "用户每天第一次登录";
	}

	/**
	 * 获取会员积分更新原因
	 */
	@Override
	protected String getMemberPointUpdateReason(Long userAccountId, Object parameter,
			Map<String, Object> updateMemberPointResult) throws Exception {
		return "用户每天第一次登录";
	}

	/**
	 * 完成更新会员信息
	 */
	@Override
	protected Boolean finishExecute(Long userAccountId, Object parameter) throws Exception {
		return true;
	}

	/**
	 * 撤销更新会员等级-登录操作无法撤销
	 */
	@Override
	protected Map<String, Object> undoUpdatedMemberLevel(Long userAccountId,
			Object parameter) throws Exception {
		return null;
	}

	/**
	 * 撤销更新会员积分
	 */
	@Override
	protected Map<String, Object> undoUpdatedMemberPoint(Long userAccountId,
			Object parameter) throws Exception {
		return null;
	}

	/**
	 * 完成撤销操作
	 */
	@Override
	protected Boolean finishUndo(Long userAccountId, Object parameter) throws Exception {
		return true;
	}

	/**
	 * 获取撤销会员等级原因
	 */
	@Override
	protected String getUndoMemberLevelUpdateReason(Long userAccountId, Object parameter,
			Map<String, Object> undoMemberLevelResult) throws Exception {
		return null;
	}

	/**
	 * 获取撤销会员积分原因
	 */
	@Override
	protected String getUndoMemberPointUpdateReason(Long userAccountId, Object parameter,
			Map<String, Object> undoMemberPointResult) throws Exception {
		return null;
	}

}
